SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO

/** Create Stored Procedures **/

CREATE PROCEDURE [dbo].[DnnProduct_CDS_DistributorConfigList]
	@SortExpression varchar(50),
	@MaximumRows int = null, 
	@StartRowIndex int = null
AS
BEGIN
	SET NOCOUNT ON;

	DECLARE @SQLString NVARCHAR(1000)
	DECLARE @ParmDefinition NVARCHAR(100)

	/* Build the SQL string once.*/
	SET @SQLString = N'WITH ItemRN AS (SELECT ROW_NUMBER() OVER(ORDER BY '+ @SortExpression + ') AS RowNum, [ID], [distID], [name], [disturl], [notifyurl], [sharedsecret], [AllowedIPAddress], [LocationDir], [DownloadURLDir], [DataPathDir], [PWDBName], [PWServerDN], [MaxDownloadsReached], [UsePW], [AllowedWebStore], [Description], [MaxLoanCount], [LinkExpires], [Deleted] FROM [DISC_DistributorConfig] WHERE [Deleted] = 0'

	SET @SQLString = @SQLString + N' ) SELECT [ID], [distID], [name], [disturl], [notifyurl], [sharedsecret], [AllowedIPAddress], [LocationDir], [DownloadURLDir], [DataPathDir], [PWDBName], [PWServerDN], [MaxDownloadsReached], [UsePW], [AllowedWebStore], [Description], [MaxLoanCount], [LinkExpires], [Deleted] FROM ItemRN WHERE RowNum BETWEEN @startRowIndex+1 AND (@startRowIndex+@maximumRows);'
	SET @ParmDefinition = N'@MaximumRows int, @StartRowIndex int'
	EXECUTE sp_executesql @SQLString, @ParmDefinition, @MaximumRows, @StartRowIndex
END
GO
